[[...path]].page.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import React from 'react';
  2. import { IUser, IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import Script from 'next/script';
  10. import CountBadge from '~/components/Common/CountBadge';
  11. import PageListIcon from '~/components/Icons/PageListIcon';
  12. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  13. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  14. import { Page } from '~/components/Page';
  15. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  16. import TableOfContents from '~/components/TableOfContents';
  17. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  18. import { CrowiRequest } from '~/interfaces/crowi-request';
  19. import { RendererConfig } from '~/interfaces/services/renderer';
  20. import { IShareLinkHasId } from '~/interfaces/share-link';
  21. import {
  22. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  23. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault,
  24. } from '~/stores/context';
  25. import { useDescendantsPageListModal } from '~/stores/modal';
  26. import loggerFactory from '~/utils/logger';
  27. import {
  28. CommonProps, getServerSideCommonProps, useCustomTitle, getNextI18NextConfig,
  29. } from '../utils/commons';
  30. const logger = loggerFactory('growi:next-page:share');
  31. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  32. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  33. type Props = CommonProps & {
  34. shareLink?: IShareLinkHasId,
  35. isExpired: boolean,
  36. disableLinkSharing: boolean,
  37. isSearchServiceConfigured: boolean,
  38. isSearchServiceReachable: boolean,
  39. isSearchScopeChildrenAsDefault: boolean,
  40. rendererConfig: RendererConfig,
  41. };
  42. const SharedPage: NextPage<Props> = (props: Props) => {
  43. useIsSearchPage(false);
  44. useShareLinkId(props.shareLink?._id);
  45. useCurrentPageId(props.shareLink?.relatedPage._id);
  46. useCurrentUser(props.currentUser);
  47. useCurrentPathname(props.currentPathname);
  48. useRendererConfig(props.rendererConfig);
  49. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  50. useIsSearchServiceReachable(props.isSearchServiceReachable);
  51. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  52. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  53. const { t } = useTranslation();
  54. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  55. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  56. const shareLink = props.shareLink;
  57. return (
  58. <>
  59. <Script type="text/javascript" src="https://www.draw.io/js/viewer.min.js"></Script>
  60. <ShareLinkLayout title={useCustomTitle(props, 'GROWI')} expandContainer={props.isContainerFluid}>
  61. <div className="h-100 d-flex flex-column justify-content-between">
  62. <header className="py-0 position-relative">
  63. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  64. </header>
  65. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  66. <div className="flex-grow-1">
  67. <div id="content-main" className="content-main">
  68. <div className="grw-container-convertible">
  69. { props.disableLinkSharing && (
  70. <div className="mt-4">
  71. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  72. </div>
  73. )}
  74. { (isNotFound && !props.disableLinkSharing) && (
  75. <div className="container-lg">
  76. <h2 className="text-muted mt-4">
  77. <i className="icon-ban" aria-hidden="true" />
  78. <span> Page is not found</span>
  79. </h2>
  80. </div>
  81. )}
  82. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  83. <div className="container-lg">
  84. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  85. <h2 className="text-muted mt-4">
  86. <i className="icon-ban" aria-hidden="true" />
  87. <span> Page is expired</span>
  88. </h2>
  89. </div>
  90. )}
  91. {(isShowSharedPage && shareLink != null) && (
  92. <>
  93. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  94. <div className="d-flex flex-column flex-lg-row-reverse">
  95. <div className="grw-side-contents-container">
  96. <div className="grw-side-contents-sticky-container">
  97. {/* Page list */}
  98. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  99. { shareLink.relatedPage.path != null && (
  100. <button
  101. type="button"
  102. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  103. rounded-pill d-flex justify-content-between align-items-center"
  104. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  105. data-testid="pageListButton"
  106. >
  107. <div className="grw-page-accessories-control-icon">
  108. <PageListIcon />
  109. </div>
  110. {t('page_list')}
  111. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  112. </button>
  113. ) }
  114. </div>
  115. <div className="d-none d-lg-block">
  116. <TableOfContents />
  117. </div>
  118. </div>
  119. </div>
  120. <div className="flex-grow-1 flex-basis-0 mw-0">
  121. <Page />
  122. </div>
  123. </div>
  124. </>
  125. )}
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  130. </ShareLinkLayout>
  131. </>
  132. );
  133. };
  134. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  135. const req: CrowiRequest = context.req as CrowiRequest;
  136. const { crowi } = req;
  137. props.disableLinkSharing = crowi.configManager.getConfig('crowi', 'security:disableLinkSharing');
  138. props.isSearchServiceConfigured = crowi.searchService.isConfigured;
  139. props.isSearchServiceReachable = crowi.searchService.isReachable;
  140. props.isSearchScopeChildrenAsDefault = crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  141. props.rendererConfig = {
  142. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  143. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  144. adminPreferredIndentSize: crowi.configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  145. isIndentSizeForced: crowi.configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  146. plantumlUri: process.env.PLANTUML_URI ?? null,
  147. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  148. drawioEmbedUri: crowi.configManager.getConfig('crowi', 'app:drawioUri'),
  149. // XSS Options
  150. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  151. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  152. tagWhiteList: crowi.xssService.getTagWhiteList(),
  153. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  154. };
  155. }
  156. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  157. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  158. props._nextI18Next = nextI18NextConfig._nextI18Next;
  159. }
  160. function getAction(props: Props): SupportedActionType {
  161. let action: SupportedActionType;
  162. if (props.isExpired) {
  163. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  164. }
  165. else if (props.shareLink == null) {
  166. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  167. }
  168. else {
  169. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  170. }
  171. return action;
  172. }
  173. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  174. const req: CrowiRequest = context.req as CrowiRequest;
  175. const parameters = {
  176. ip: req.ip,
  177. endpoint: req.originalUrl,
  178. action,
  179. user: req.user?._id,
  180. snapshot: {
  181. username: req.user?.username,
  182. },
  183. };
  184. await req.crowi.activityService.createActivity(parameters);
  185. }
  186. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  187. const req = context.req as CrowiRequest<IUserHasId & any>;
  188. const { crowi, params } = req;
  189. const result = await getServerSideCommonProps(context);
  190. if (!('props' in result)) {
  191. throw new Error('invalid getSSP result');
  192. }
  193. const props: Props = result.props as Props;
  194. try {
  195. const ShareLinkModel = crowi.model('ShareLink');
  196. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  197. if (shareLink != null) {
  198. props.isExpired = shareLink.isExpired();
  199. props.shareLink = shareLink.toObject();
  200. }
  201. }
  202. catch (err) {
  203. logger.error(err);
  204. }
  205. injectServerConfigurations(context, props);
  206. await injectNextI18NextConfigurations(context, props);
  207. await addActivity(context, getAction(props));
  208. return {
  209. props,
  210. };
  211. };
  212. export default SharedPage;